package com.study.signin_demo2.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.study.signin_demo2.entity.User;
import com.study.signin_demo2.mapper.UserMapper;
import com.study.signin_demo2.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;


/**
 * @author whx
 * @date 2022/2/17
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements IUserService{

    @Autowired
    UserMapper userMapper;

    @Override
    public Boolean login(String username, String password) {
        return "root".equals(username) && "123456".equals(password);
    }

    @Override
    public Boolean loginWithMysql(String username, String password) {
        if(username == null || "".equals(username)){
            log.warn("账号为空");
            return false;
        }
        if(password == null || "".equals(password)){
            log.warn("密码为空");
            return false;
        }
//        User user = userMapper.getUserByUserName(username);
//        User user = this.getOne(new QueryWrapper<User>().select("user_name", "password").eq("user_name", username));
//        User user = userMapper.selectOne(new QueryWrapper<User>().select("user_name", "password").eq("user_name", username));
        User user = userMapper.selectOne(Wrappers.<User>lambdaQuery().select(User::getUsername,User::getPassword).eq(User::getUsername,username));
        return null != user && password.equals(user.getPassword());
    }

    @Override
    public void removeByUsername(String username) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("user_name",username);
        this.removeByMap(map);
    }


    @Override
    public void removeByIds(String ids) {
        // "1,2,3,7"
        String[] idArr = ids.split(",");
        // [1,2,3,7]
        this.removeByIds(Arrays.stream(idArr).collect(Collectors.toList()));
    }



}
